﻿package com.dong.mcd.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CollectionUtil {

	/**
	 * 将集合转换成字符串
	 * @param list 集合
	 * @param splt 分隔符 默认逗号
	 * @param start 前缀
	 * @param end 后缀
	 * @param wrap 包裹符
	 * @return 字符串
	 */
	public static String listToString(List<?> list,String splt,String start,String end,String wrap){
		splt = splt==null?",":splt;
		start = start==null?"":start;
		end = end==null?"":end;
		wrap = wrap==null?"":wrap;
		
		if (CollectionUtil.isEmpty(list)){
			return start+end;
		}
		int iMax = list.size() - 1;
		
		StringBuilder b = new StringBuilder();
		b.append(start);
		for (int i = 0;; i++) {
            b.append(wrap+list.get(i)+wrap);
            if (i == iMax)
            	return b.append(end).toString();
            b.append(splt);
        }
	}
	
	public static String listToDou(List<?> list){
		return listToString(list,",",null,null,null);
	}
	
	public static String listJoin(List<?> list,String sp){
		return listToString(list,sp,null,null,null);
	}
	
	public static String listToString(List<?> list){
		return listToString(list,"",null,null,null);
	}
	
	public static String arrayToString(List<?> list){
		return listToString(list,"",null,null,null);
	}
	
	
	/**
	 * 判断集合是否为空
	 * @param collection
	 * @return
	 */
	public static boolean isEmpty(Collection<?> collection)
    {
        return collection == null || collection.isEmpty();
    }
	
	/**
	 * 对象转为map
	 * @param o
	 * @return
	 * @throws Exception
	 */
	public static Map<String,Object> objectToMap(Object o) throws Exception{
		Class<?> class1 = o.getClass();
		Field[] objFields = class1.getDeclaredFields();
		Map<String,Object> map = new HashMap<String, Object>();
		for (int i = 0; i < objFields.length; i++) {
			Field field = objFields[i];
			String fieldName = field.getName(),
					getMethodName = "get"+fieldName.substring(0, 1).toUpperCase()
					+fieldName.substring(1, fieldName.length());
			Method method = class1.getMethod(getMethodName);
			Object rv = method.invoke(o);
			if (rv instanceof Collection<?>)
				map.put(fieldName, listToDou((List<?>)rv));
			else
				map.put(fieldName, rv);
		}
		return map;
	}
	
	/**
	 * map转为对象
	 * @param <T>
	 * @param map
	 * @param class1
	 * @return
	 * @throws Exception
	 */
	public static <T> T objectToMap(Map<String,Object> map,Class<T> class1) throws Exception{
		T o = class1.newInstance();
		Field[] objFields = class1.getDeclaredFields();
		for (int i = 0; i < objFields.length; i++) {
			Field field = objFields[i];
			String fieldName = field.getName(),
					getMethodName = "set"+fieldName.substring(0, 1).toUpperCase()
					+fieldName.substring(1, fieldName.length());
			Object v = map.get(fieldName);
			Method method = class1.getMethod(getMethodName,field.getType());
			method.invoke(o,v);
		}
		return o;
	}
	
}
